package com.contact;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;

import com.android.ocean.util.common.StringUtil;
import com.android.ocean.util.common.TranHelper;
import com.common.method.CommonData;
import com.common.method.FromFlag;
import com.common.method.LocData;
import com.http.UserSearchTask;
import com.matang.R;
import com.modle.User;
import com.util.AppUtil;

public class UserSearchActivity extends BaseActivity implements
		OnClickListener, UserSearchTask.CallBack {
	Button submitBtn;
	EditText keyWordsEt,yrNumEt;
	TextView livelaceTv,skillTv;
	Spinner ageLowerSpinner;
	Spinner ageHigherSpinner;
	RadioButton rdMale, rdFemale;
	String keywords,yrNumStr,skillStr;
	String livePlaceStr;
	String lowAgeStr, highAgeStr;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_user_search);

		init();
	}

	void init() {
		mContext = UserSearchActivity.this;
		findViews();
		initPaneData();

	}

	public void initPaneData() {
		super.initPaneData();
		submitBtn.setOnClickListener(this);
		header.headTitleTv.setText("搜索好友");
		livelaceTv.setOnClickListener(this);
		skillTv.setOnClickListener(this);
	}

	@Override
	protected void findViews() {
		super.findViews();
		keyWordsEt = (EditText) findViewById(R.id.keywords_et);
		yrNumEt = (EditText) findViewById(R.id.yrnum_et);
		skillTv = (TextView) findViewById(R.id.skill_tv);
		livelaceTv = (TextView) findViewById(R.id.place_tv);
		ageLowerSpinner = (Spinner) findViewById(R.id.age_lower_spinner);
		ageHigherSpinner = (Spinner) findViewById(R.id.age_higher_spinner);
		rdMale = (RadioButton) findViewById(R.id.rd_male);
		rdFemale = (RadioButton) findViewById(R.id.rd_fmale);
		submitBtn = (Button) findViewById(R.id.submit_btn);
	}

	@Override
	protected void onNewIntent(Intent intent) {

		Bundle b = intent.getExtras();
		if (b != null) {
			String birthPlaceStr = b.getString("placeStr");
			if (!StringUtil.isNull(birthPlaceStr)) {
				livelaceTv.setText(birthPlaceStr);
			}
			String isGps = b.getString("GPS");
			if (!StringUtil.isNull(isGps)) {
				livelaceTv.setText(isGps);
			}
			String skillStr= b.getString("skill");
			if (!StringUtil.isNull(skillStr)) {
				skillTv.setText(skillStr);
			}
			
		}
		super.onNewIntent(intent);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.submit_btn: {
			submit();

			break;
		}
		case R.id.place_tv: {
			Bundle b = new Bundle();
			b.putInt("fromFlag", FromFlag.PLACE_F_USERSEARCH);
			TranHelper.toAct(mContext, BasePlaceChooseActivity.class, b);
			break;
		}
		case R.id.skill_tv: {
			Bundle b = new Bundle();
			b.putInt("fromFlag", FromFlag.SKILL_F_USERSEARCH);
			//TranHelper.toAct(mContext, SkillChooseActivity.class, b);
			break;
		}
		}

	}

	private void submit() {
		HashMap<String, Object> map = new HashMap<String, Object>();
		keywords = keyWordsEt.getText().toString();
		livePlaceStr = livelaceTv.getText().toString();
		yrNumStr = yrNumEt.getText().toString();
		skillStr = skillTv.getText().toString();
		if (!StringUtil.isNull(keywords)) {

			map.put("mobile", keywords);
		}
		if (!StringUtil.isNull(livePlaceStr)) {
			if(livePlaceStr.equalsIgnoreCase(CommonData.GPS)){
             map.put("points", LocData.getX()+","+LocData.getY());	

			}else{
			map.put("liveplace", livePlaceStr);
			}
		}
		if (!StringUtil.isNull(yrNumStr)) {
			
			map.put("systemNum", yrNumStr);
		}
		if (!StringUtil.isNull(skillStr)) {
			
			map.put("skill", skillStr);
		}

		lowAgeStr = ageLowerSpinner.getSelectedItem().toString();
		highAgeStr = ageHigherSpinner.getSelectedItem().toString();
		if (!StringUtil.isNull(lowAgeStr)&&!StringUtil.isNull(highAgeStr)) {

			map.put("age", lowAgeStr+","+highAgeStr);
		}else
		if (!StringUtil.isNull(lowAgeStr)&&StringUtil.isNull(highAgeStr)) {
			
			map.put("age", lowAgeStr+",100");
		}else
		if (StringUtil.isNull(lowAgeStr)&&!StringUtil.isNull(highAgeStr)) {
			
			map.put("age", "0,"+highAgeStr);
		}
//		if (rdMale.isChecked()) {
//			map.put("sex", "m");
//		} else {
//			map.put("sex", "f");
//		}

	showProgressDialog("搜索中...");
		new UserSearchTask(this).execute(map);
	}

	@Override
	public void procError(String msg) {
	dismissProgressDialog();
	AppUtil.tsMsg(mContext, msg);

	}

	@Override
	public void procUserSearchList(ArrayList<User> list) {
		dismissProgressDialog();
		
		if(StringUtil.isListNull(list)){
			AppUtil.tlMsg(mContext, "没有相关信息");
		}else{
			Bundle b = new Bundle();
			b.putSerializable("userList", list);
			TranHelper.toAct(mContext, YrListActivity.class, b);
			finish();
			
		}
	}

}
